﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HotelMatchUtility.Entity;
using System.Data;
using Hotel.Vendor.HotelStaticInfoManagement.BookingHotelFilter;

namespace HotelMatchUtility.Utility
{
    public class Y_FileOutputer
    {
        public static void DoOutput(List<HotelEntity> hotelList, string fileName)
        {
            DataTable dtMatched = InnitialMatchedCsvFile();
            int ArrayId= 1 ;
            foreach (var hotelEntity in hotelList)
            {
                AddHotelEntityToTable(hotelEntity, dtMatched, ArrayId);
                foreach (var exactEntity in hotelEntity.ExactMatchResult)
                {
                    AddHotelEntityToTable(exactEntity, dtMatched, ArrayId);
                }
                foreach (var simEntity in hotelEntity.SimilarMatchResult)
                {
                    AddHotelEntityToTable(simEntity, dtMatched, ArrayId);
                }
                ArrayId++;
            }
            DoCsvFileLink(dtMatched, fileName);
        }

        public static void AddHotelEntityToTable(HotelEntity entity, DataTable dt, int ArrayID)
        {
            if (entity != null)
            {
                DataRow dr = dt.NewRow();
                dr["ArrayID"] = ArrayID;
                dr["MatchType"] = entity.MatchType;
                dr["Hotel"] = entity.Hotel;
                dr["类别"] = entity.HotelType;
                dr["HotelBelongTo"] = entity.HotelBelongTo;
                dr["有无房价"] = entity.PriceInfo;
                // dr["HotelGroupName"] = entity.HotelGroupName;
                dr["是否打开（可订）"] = entity.Bookable;
                dr["HotelName"] = entity.HotelName;
                dr["HotelEnName"] = entity.HotelEnName;
                dr["Address"] = entity.Address;
                dr["Telephone"] = entity.Telephone;
                dr["ZipCode"] = entity.ZipCode;
                dr["City"] = entity.City;
                dr["CityName"] = entity.CityName;
                dr["CityEnName"] = entity.CityEnName;
                
                dr["Province"] = entity.Province;
                dr["ProvinceName"] = entity.ProvinceName;
                dr["ProvinceEnName"] = entity.ProvinceEnName;

                dr["Country"] = entity.Country;
                dr["CountryName"] = entity.CountryName;
                dr["CountryEnName"] = entity.CountryEnName;
                dr["Glat"] = entity.Glat;
                dr["Glon"] = entity.Glon;
                dr["HotelGroupHotelCode"] = entity.HotelCode;
                dr["DistrictCodeOrId"] = entity.DistrictCodeBookingOrCtrip;
                dr["DistrictName"] = entity.DistrictName;
                dr["DistrictEnName"] = entity.DistrictEnName;
                dt.Rows.Add(dr);
            }
        }

        public static DataTable InnitialMatchedCsvFile()
        {
            DataTable dtMatched = new DataTable();//匹配
            dtMatched.Columns.Add("ArrayID", typeof(int));
            dtMatched.Columns.Add("MatchType", typeof(string));
            dtMatched.Columns.Add("Hotel", typeof(int));//

            dtMatched.Columns.Add("类别", typeof(string));
            dtMatched.Columns.Add("HotelBelongTo", typeof(string));
            dtMatched.Columns.Add("有无房价", typeof(string));
            //dtMatched.Columns.Add("HotelGroupName", typeof(string));

            dtMatched.Columns.Add("是否打开（可订）", typeof(string));

            dtMatched.Columns.Add("HotelName", typeof(string));//
            dtMatched.Columns.Add("HotelEnName", typeof(string));

            dtMatched.Columns.Add("Address", typeof(string));
            dtMatched.Columns.Add("Telephone", typeof(string));
            dtMatched.Columns.Add("ZipCode", typeof(string));

            dtMatched.Columns.Add("City", typeof(int));
            dtMatched.Columns.Add("CityName", typeof(string));
            dtMatched.Columns.Add("CityEnName", typeof(string));

            dtMatched.Columns.Add("Province", typeof(int));
            dtMatched.Columns.Add("ProvinceName", typeof(string));
            dtMatched.Columns.Add("ProvinceEnName", typeof(string));

            dtMatched.Columns.Add("Country", typeof(int));
            dtMatched.Columns.Add("CountryName", typeof(string));
            dtMatched.Columns.Add("CountryEnName", typeof(string));

            dtMatched.Columns.Add("Glat", typeof(double));
            dtMatched.Columns.Add("Glon", typeof(double));

            dtMatched.Columns.Add("HotelGroupHotelCode", typeof(string));

            dtMatched.Columns.Add("DistrictCodeOrId", typeof(string));
            dtMatched.Columns.Add("DistrictName", typeof(string));
            dtMatched.Columns.Add("DistrictEnName", typeof(string));

            return dtMatched;
        }

        /// <summary>
        /// CSV文件生成 文件链接生成
        /// </summary>
        private static void DoCsvFileLink(DataTable dtMatched, string fileName)
        {
            string csvMatchedFileName = "";
            string rootPath = System.AppDomain.CurrentDomain.BaseDirectory;

            //匹配文件生成
            if (dtMatched.Rows.Count > 0)
            {
                //生成CSV文件
                csvMatchedFileName = FileManagement.GetCSVFileName(rootPath, fileName + ".csv");
                FileManagement.DataTableToCSV(csvMatchedFileName, dtMatched);
            }
        }
    }
}
